<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="客户名称" prop="customName">
              <el-input v-model="queryParams.customName" placeholder="请输入客户名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="件数" prop="number">
              <el-input v-model="queryParams.number" placeholder="请输入件数" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="单位面积对应的金额" prop="areaPrice">
              <el-input v-model="queryParams.areaPrice" placeholder="请输入单位面积对应的金额" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="单价" prop="unitPrice">
              <el-input v-model="queryParams.unitPrice" placeholder="请输入单价" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="单件面积" prop="area">
              <el-input v-model="queryParams.area" placeholder="请输入单件面积" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="总价，所有件数的运费合计" prop="totalPrice">
              <el-input v-model="queryParams.totalPrice" placeholder="请输入总价，所有件数的运费合计" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="客户id" prop="customId">
              <el-input v-model="queryParams.customId" placeholder="请输入客户id" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="已支付金额" prop="paidAmount">
              <el-input v-model="queryParams.paidAmount" placeholder="请输入已支付金额" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="接单人" prop="receivingOrderId">
              <el-input v-model="queryParams.receivingOrderId" placeholder="请输入接单人" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="接单时间" prop="receivingOrderTime">
              <el-date-picker v-model="queryParams.receivingOrderTime" clearable type="date" value-format="YYYY-MM-DD" placeholder="请选择接单时间" />
            </el-form-item>
            <el-form-item label="派车人id" prop="dispatchCarId">
              <el-input v-model="queryParams.dispatchCarId" placeholder="请输入派车人id" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="接单人名称" prop="receivingOrderName">
              <el-input v-model="queryParams.receivingOrderName" placeholder="请输入接单人名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="派车人名称" prop="dispatchCarName">
              <el-input v-model="queryParams.dispatchCarName" placeholder="请输入派车人名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="派车人时间" prop="dispatchCarTime">
              <el-date-picker v-model="queryParams.dispatchCarTime" clearable type="date" value-format="YYYY-MM-DD" placeholder="请选择派车人时间" />
            </el-form-item>
            <el-form-item label="财务人id" prop="treasurerId">
              <el-input v-model="queryParams.treasurerId" placeholder="请输入财务人id" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="财务操作时间" prop="treasurerTime">
              <el-date-picker v-model="queryParams.treasurerTime" clearable type="date" value-format="YYYY-MM-DD" placeholder="请选择财务操作时间" />
            </el-form-item>
            <el-form-item label="财务人名称" prop="treasurerName">
              <el-input v-model="queryParams.treasurerName" placeholder="请输入财务人名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['wuliu:wares:add']" type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['wuliu:wares:edit']" type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['wuliu:wares:remove']" type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['wuliu:wares:export']" type="warning" plain icon="Download" @click="handleExport">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="waresList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column v-if="true" label="" align="center" prop="id" />
        <el-table-column label="客户名称" align="center" prop="customName" />
        <el-table-column label="件数" align="center" prop="number" />
        <el-table-column label="金额计算类别" align="center" prop="priceType" />
        <el-table-column label="单位面积对应的金额" align="center" prop="areaPrice" />
        <el-table-column
          label="单价"
          align="center"
          prop="
unitPrice"
        />
        <el-table-column label="单件面积" align="center" prop="area" />
        <el-table-column label="总价，所有件数的运费合计" align="center" prop="totalPrice" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="客户id" align="center" prop="customId" />
        <el-table-column label="支付状态" align="center" prop="paymentStatus" />
        <el-table-column label="已支付金额" align="center" prop="paidAmount" />
        <el-table-column label="报单状态" align="center" prop="status" />
        <el-table-column label="接单人" align="center" prop="receivingOrderId" />
        <el-table-column label="接单时间" align="center" prop="receivingOrderTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.receivingOrderTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="派车人id" align="center" prop="dispatchCarId" />
        <el-table-column label="接单人名称" align="center" prop="receivingOrderName" />
        <el-table-column label="派车人名称" align="center" prop="dispatchCarName" />
        <el-table-column label="派车人时间" align="center" prop="dispatchCarTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.dispatchCarTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="财务人id" align="center" prop="treasurerId" />
        <el-table-column label="财务操作时间" align="center" prop="treasurerTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.treasurerTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="财务人名称" align="center" prop="treasurerName" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button v-hasPermi="['wuliu:wares:edit']" link type="primary" icon="Edit" @click="handleUpdate(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button v-hasPermi="['wuliu:wares:remove']" link type="primary" icon="Delete" @click="handleDelete(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
    <!-- 添加或修改报单表对话框 -->
    <el-dialog v-model="dialog.visible" :title="dialog.title" width="500px" append-to-body>
      <el-form ref="waresFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客户名称" prop="customName">
          <el-input v-model="form.customName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="件数" prop="number">
          <el-input v-model="form.number" placeholder="请输入件数" />
        </el-form-item>
        <el-form-item label="单位面积对应的金额" prop="areaPrice">
          <el-input v-model="form.areaPrice" placeholder="请输入单位面积对应的金额" />
        </el-form-item>
        <el-form-item
          label="单价"
          prop="
unitPrice"
        >
          <el-input v-model="form.unitPrice" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="单件面积" prop="area">
          <el-input v-model="form.area" placeholder="请输入单件面积" />
        </el-form-item>
        <el-form-item label="总价，所有件数的运费合计" prop="totalPrice">
          <el-input v-model="form.totalPrice" placeholder="请输入总价，所有件数的运费合计" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="客户id" prop="customId">
          <el-input v-model="form.customId" placeholder="请输入客户id" />
        </el-form-item>
        <el-form-item label="已支付金额" prop="paidAmount">
          <el-input v-model="form.paidAmount" placeholder="请输入已支付金额" />
        </el-form-item>
        <el-form-item label="接单人" prop="receivingOrderId">
          <el-input v-model="form.receivingOrderId" placeholder="请输入接单人" />
        </el-form-item>
        <el-form-item label="接单时间" prop="receivingOrderTime">
          <el-date-picker v-model="form.receivingOrderTime" clearable type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择接单时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="派车人id" prop="dispatchCarId">
          <el-input v-model="form.dispatchCarId" placeholder="请输入派车人id" />
        </el-form-item>
        <el-form-item label="接单人名称" prop="receivingOrderName">
          <el-input v-model="form.receivingOrderName" placeholder="请输入接单人名称" />
        </el-form-item>
        <el-form-item label="派车人名称" prop="dispatchCarName">
          <el-input v-model="form.dispatchCarName" placeholder="请输入派车人名称" />
        </el-form-item>
        <el-form-item label="派车人时间" prop="dispatchCarTime">
          <el-date-picker v-model="form.dispatchCarTime" clearable type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择派车人时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="财务人id" prop="treasurerId">
          <el-input v-model="form.treasurerId" placeholder="请输入财务人id" />
        </el-form-item>
        <el-form-item label="财务操作时间" prop="treasurerTime">
          <el-date-picker v-model="form.treasurerTime" clearable type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择财务操作时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="财务人名称" prop="treasurerName">
          <el-input v-model="form.treasurerName" placeholder="请输入财务人名称" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Wares" lang="ts">
import { listWares, getWares, delWares, addWares, updateWares } from '@/api/wuliu/wares';
import { WaresVO, WaresQuery, WaresForm } from '@/api/wuliu/wares/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const waresList = ref<WaresVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const waresFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: WaresForm = {
  id: undefined,
  customName: undefined,
  number: undefined,
  priceType: undefined,
  areaPrice: undefined,

  unitPrice: undefined,
  area: undefined,
  totalPrice: undefined,
  remark: undefined,
  customId: undefined,
  paymentStatus: undefined,
  paidAmount: undefined,
  status: undefined,
  receivingOrderId: undefined,
  receivingOrderTime: undefined,
  dispatchCarId: undefined,
  receivingOrderName: undefined,
  dispatchCarName: undefined,
  dispatchCarTime: undefined,
  treasurerId: undefined,
  treasurerTime: undefined,
  treasurerName: undefined
};
const data = reactive<PageData<WaresForm, WaresQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    customName: undefined,
    number: undefined,
    priceType: undefined,
    areaPrice: undefined,

    unitPrice: undefined,
    area: undefined,
    totalPrice: undefined,
    customId: undefined,
    paymentStatus: undefined,
    paidAmount: undefined,
    status: undefined,
    receivingOrderId: undefined,
    receivingOrderTime: undefined,
    dispatchCarId: undefined,
    receivingOrderName: undefined,
    dispatchCarName: undefined,
    dispatchCarTime: undefined,
    treasurerId: undefined,
    treasurerTime: undefined,
    treasurerName: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '不能为空', trigger: 'blur' }],
    customName: [{ required: true, message: '客户名称不能为空', trigger: 'blur' }],
    number: [{ required: true, message: '件数不能为空', trigger: 'blur' }],
    priceType: [{ required: true, message: '金额计算类别不能为空', trigger: 'change' }],
    areaPrice: [{ required: true, message: '单位面积对应的金额不能为空', trigger: 'blur' }],

    unitPrice: [{ required: true, message: '单价不能为空', trigger: 'blur' }],
    area: [{ required: true, message: '单件面积不能为空', trigger: 'blur' }],
    totalPrice: [{ required: true, message: '总价，所有件数的运费合计不能为空', trigger: 'blur' }],
    remark: [{ required: true, message: '备注不能为空', trigger: 'blur' }],
    customId: [{ required: true, message: '客户id不能为空', trigger: 'blur' }],
    paymentStatus: [{ required: true, message: '支付状态不能为空', trigger: 'change' }],
    paidAmount: [{ required: true, message: '已支付金额不能为空', trigger: 'blur' }],
    status: [{ required: true, message: '报单状态不能为空', trigger: 'change' }],
    receivingOrderId: [{ required: true, message: '接单人不能为空', trigger: 'blur' }],
    receivingOrderTime: [{ required: true, message: '接单时间不能为空', trigger: 'blur' }],
    dispatchCarId: [{ required: true, message: '派车人id不能为空', trigger: 'blur' }],
    receivingOrderName: [{ required: true, message: '接单人名称不能为空', trigger: 'blur' }],
    dispatchCarName: [{ required: true, message: '派车人名称不能为空', trigger: 'blur' }],
    dispatchCarTime: [{ required: true, message: '派车人时间不能为空', trigger: 'blur' }],
    treasurerId: [{ required: true, message: '财务人id不能为空', trigger: 'blur' }],
    treasurerTime: [{ required: true, message: '财务操作时间不能为空', trigger: 'blur' }],
    treasurerName: [{ required: true, message: '财务人名称不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询报单表列表 */
const getList = async () => {
  loading.value = true;
  const res = await listWares(queryParams.value);
  waresList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  waresFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: WaresVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加报单表';
};

/** 修改按钮操作 */
const handleUpdate = async (row?: WaresVO) => {
  reset();
  const _id = row?.id || ids.value[0];
  const res = await getWares(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改报单表';
};

/** 提交按钮 */
const submitForm = () => {
  waresFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateWares(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addWares(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: WaresVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除报单表编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delWares(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'wuliu/wares/export',
    {
      ...queryParams.value
    },
    `wares_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
});
</script>
